/// @tags: ConvexOptimization
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

namespace BlueQuantum {

typedef long long LL;

int const N = 3e5 + 5, INF = 0x3f3f3f3f;
LL const LIM = 1e12, LINF = 0x3f3f3f3f3f3f3f3f;

int n, k, e_cnt;
LL mid;
int heads[N];

struct Edge {
  int v, nxt, w;
} e[N << 1];

struct Node {
  LL val;
  int x;

  Node(LL val = -LINF, int x = INF) : val(val), x(x) {}
  inline bool operator<(Node const& rhs) const {
    return val == rhs.val ? x > rhs.x : val < rhs.val;
  }
  inline Node operator+(Node const& rhs) const {
    return (Node){val + rhs.val, x + rhs.x};
  }
} f[N][3];

inline void add(int u, int v, int w) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt, e[e_cnt].w = w;
}

void solve(int u, int fa) {
  f[u][0] = (Node){0, 0}, f[u][1] = Node(), f[u][2] = (Node){-mid, 1};
  for (int i = heads[u], v; i; i = e[i].nxt)
    if ((v = e[i].v) != fa) {
      solve(v, u);
      Node tmp[3];
      for (int j = 0; j < 3; ++j) tmp[0] = max(tmp[0], f[u][0] + f[v][j]);
      tmp[1] = max(tmp[1], f[u][0] + f[v][0] + (Node){e[i].w - mid, 1});
      tmp[1] = max(tmp[1], f[u][0] + f[v][1] + (Node){e[i].w, 0});
      for (int j = 0; j < 3; ++j) tmp[1] = max(tmp[1], f[u][1] + f[v][j]);
      tmp[2] = max(tmp[2], f[u][1] + f[v][0] + (Node){e[i].w, 0});
      tmp[2] = max(tmp[2], f[u][1] + f[v][1] + (Node){e[i].w + mid, -1});
      for (int j = 0; j < 3; ++j) tmp[2] = max(tmp[2], f[u][2] + f[v][j]);
      for (int j = 0; j < 3; ++j) f[u][j] = tmp[j];
    }
}

inline int main() {
  cin >> n >> k, k++;
  for (int i = 1, u, v, w; i < n; ++i) {
    cin >> u >> v >> w;
    add(u, v, w), add(v, u, w);
  }
  LL l = -LIM, r = LIM;
  while (l < r) {
    mid = floor((l + r) / 2.0);
    solve(1, 1);
    Node tmp = max(f[1][0], max(f[1][1], f[1][2]));
    tmp.x > k ? l = mid + 1 : r = mid;
  }
  mid = l;
  solve(1, 1);
  Node tmp = max(f[1][0], max(f[1][1], f[1][2]));
  cout << tmp.val + k * mid;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P4383 [八省联考2018] 林克卡特树.in", "r", stdin);
  freopen("P4383 [八省联考2018] 林克卡特树.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
